package com.android.myapplication.view.media;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;

import com.android.myapplication.ITest;

public class TestActvity extends Activity {
    ITest iTest;
    public void onCreate(Bundle bundle){
        super.onCreate(bundle);
        bindService(new Intent(TestActvity.this, TestSerivce.class), connection, BIND_AUTO_CREATE);
    }

    private ServiceConnection connection = new ServiceConnection() {
        /**
         * 连接到服务
         * @param name
         * @param service
         */
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            iTest = (ITest) service;
            try {
                iTest.sendMessage("hello");
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }

        /**
         * 断开连接
         * @param name
         */
        @Override
        public void onServiceDisconnected(ComponentName name) {
        }
    };
}
